package cn.life.business.literature.dao.impl;

import cn.life.common.entity.vo.Pagination;
import cn.life.common.support.jdbc.AbstractSupportDao;
import cn.life.business.literature.dao.LiteratureDao;
import cn.life.business.literature.entity.po.Literature;
import cn.life.business.literature.entity.vo.LiteratuerVo;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * Created by Wangzhenyu on Date
 */
@Repository
public class LiteratureDaoImpl  extends AbstractSupportDao implements LiteratureDao {

    @Override
    public int addLiterature(Literature literature) {
        String sql="INSERT INTO factory_literature(literature_name,literature_desc,file_url,opr_user_id,opr_user_name,opr_date) VALUES(?,?,?,?,?,?)";
        return update(sql,literature.getLiteratureName(),literature.getLiteratureDesc(),literature.getFileUrl(),
                literature.getOprUserId(),literature.getOprUserName(),literature.getOprDate());
    }

    @Override
    public int deleteLiterature(long literatureId) {
        String sql="delete from factory_literature where literature_id=?";
        return update(sql,literatureId);
    }

    @Override
    public int updateLiterature(Literature literature) {
        String  sql="update factory_literature set literature_name=?," + "literature_desc=?,file_url=? where " +
                "literature_id=? ";
        return update(sql,literature.getLiteratureName(),literature.getLiteratureDesc(),literature.getFileUrl(),
                literature.getLiteratureId());
    }

    @Override
    public Pagination<LiteratuerVo> getAllLiteratureInfo(LiteratuerVo literatuerVo) {
        StringBuffer stringBuffer=new StringBuffer("select * from factory_literature where 1=1");
        if (!"".equals(literatuerVo.getLiteratureName())&&literatuerVo.getLiteratureName()!=null){
            stringBuffer.append(" and literature_name like'%"+literatuerVo.getLiteratureName()+"%' ");
        }
        stringBuffer.append(" ORDER BY opr_date DESC");

        return queryForListByPageEx(stringBuffer.toString(),LiteratuerVo.class,literatuerVo.getPageIndex(),literatuerVo.getPageSize());
    }

    @Override
    public LiteratuerVo getLiteratuer(LiteratuerVo literatuerVo) {
        String sql= "select * from factory_literature where literature_id=? ";
        return queryForObject(sql,LiteratuerVo.class,literatuerVo.getLiteratureId());
    }

    @Override
    public void addLiteratuerNumber(LiteratuerVo literatuerVo) {
        String sql = "update factory_literature a inner join factory_literature b on a.literature_id = b.literature_id and b.literature_id = ? set a.number = (b.number + 1) where a.literature_id = ? ";
        update(sql,literatuerVo.getLiteratureId(),literatuerVo.getLiteratureId());
    }
}
